package beautiful.butterfly.drds.binlog.parser;


import beautiful.butterfly.drds.binlog.connection.ConnectionAttributes;
import beautiful.butterfly.drds.binlog.event.EventData;
import beautiful.butterfly.drds.binlog.event.data.DeleteRowsEventData;
import beautiful.butterfly.drds.binlog.event.data.TableMapEventData;
import beautiful.butterfly.drds.binlog.netty.server.NioSocketChannelExtend;
import beautiful.butterfly.drds.binlog.utils.ByteUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.Serializable;
import java.util.BitSet;
import java.util.LinkedList;
import java.util.List;

@Slf4j
public class DeleteRowsEventDataParser implements EventDataParser
{


    private boolean mayContainExtraInformation = false;

    public DeleteRowsEventDataParser(boolean mayContainExtraInformation)
    {
        this.mayContainExtraInformation = mayContainExtraInformation;
    }

    @Override
    public EventData parse(ByteBuf byteBuf, ChannelHandlerContext channelHandlerContext, int checksumLength)
    {
        // LoggerUtils.debug(logger, "");
        long tableId = ByteUtils.readUnsignedLong(byteBuf, 6);
        byteBuf.skipBytes(2);//// reserved
        // 分支
        if (mayContainExtraInformation)
        {
            int extraInfoLength = ByteUtils.readUnsignedInt(byteBuf, 2); // inputStream.readInteger(2);
            byteBuf.skipBytes(extraInfoLength - 2);
        }
        // 继续处理
        int numberOfColumns = ByteUtils.readVariableNumber(byteBuf).intValue();
        BitSet includedColumns = null;
        try
        {
            includedColumns = ByteUtils.readBitSet(byteBuf, numberOfColumns, true);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        // 继续处理
        List<Serializable[]> rows = new LinkedList<Serializable[]>();
        ConnectionAttributes connectionAttributes = ((NioSocketChannelExtend) channelHandlerContext.channel()).getConnectionAttributes();
        TableMapEventData tableMapEventData = connectionAttributes.getTableMapEventData(tableId);
        if (null == tableMapEventData)
        {
            log.info("error,fail to find tableMapEventData ,tableId is:" + tableId);
        }
        String databaseName = tableMapEventData.getDatabaseName();
        String tableName = tableMapEventData.getTableName();
        try
        {
            while (byteBuf.readableBytes() > checksumLength)
            {// 表明还有内容可取
                rows.add(ByteUtils.deserializeRow(tableMapEventData, includedColumns, byteBuf));
            }

        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 返回最终数据
        DeleteRowsEventData deleteRowsEventData = new DeleteRowsEventData();
        deleteRowsEventData.setTableMapEventData(tableMapEventData);
        deleteRowsEventData.setTableId(tableId);
        if (connectionAttributes.acceptByFilter(databaseName, tableName))
        {
            deleteRowsEventData.setIncludedColumns(includedColumns, connectionAttributes.getPositionToColumnNameMap(databaseName, tableName));
        }
        deleteRowsEventData.setRows(rows);
        return deleteRowsEventData;
    }

}
